package com.webapp.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.webapp.model.User;
import com.webapp.model.ViewTableModel;

/**
 * Servlet implementation class ViewServlet
 */
public class ViewServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		
		User user = (User)session.getAttribute("UserObject"); 
		
		// get user name just incase
		String userName = user.getUserName();
		
		// create new table model
		ViewTableModel viewM = new ViewTableModel();
		
		// get the table and store it in session
		ArrayList myList = viewM.getTable();
		
		if(myList != null) {
			// set it to the sesssion
			session.setAttribute("Table", myList);
			System.out.print("in here");
			// request dispatcher
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/viewApps.jsp");
			
			dispatcher.forward(request, response);
		} else {
			List<String> errorList = new ArrayList();
			errorList.add("Application Gathering Issue");
			errorList.add("ViewTableModel");
			session.setAttribute("ErrorObject",errorList);
			response.sendRedirect("error.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		
		/**
		 * Get the searching user parameters
		 */
		String user =  request.getParameter("userSearch");
		if(user != null || user.equals("")) {
			session.setAttribute("searchUser", user);
		}
		
		/**
		 * get the users applications in a list
		 */
	}

}
